<?php

/**
* HuntCodeModule.class.php
*
* Module responsable for code hunt
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once("Auth/Auth.php");
require_once("framework/config.php");
require_once("UserAuth.php");
require_once("framework/PageModule.class.php");
require_once("HTML/QuickForm.php");
require_once("HTML/QuickForm/Renderer/Default.php");
require_once("HTML/Template/IT.php");


// {{{ AuthForm

class HuntCodeModule extends PageModule 
{
    
    
    private $dbh = null;

    function HuntCodeModule()
    {

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $this->dbh = new PDO($options['dsn_string']);
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    }

    // {{{ getContent()

    
    /**
    * getContent()
    *
    */

    function getContent()
    {

        trigger_error("ENTER HuntCodeModule::getContent()", E_USER_NOTICE);

        $options = &PEAR::getStaticProperty('CONFIG_INI','TEMPLATES');        
        $tpl = new HTML_Template_IT($options["templates_dir"]);
        $tpl->loadTemplatefile("hunted_code.tpl.xhtml",true,true);


        $statusTpl = new HTML_Template_IT($options["templates_dir"]);
        $statusTpl->loadTemplatefile("status.tpl.xhtml",FALSE,FALSE);



        if ( $this->isValid($_GET['code']) )
        {
            trigger_error("HuntCodeModule::getContent() Code is valid.", E_USER_NOTICE);
            if( !$this->isHunted($_GET['code']) )
            {   
                trigger_error("HuntCodeModule::getContent() Code is hunted.", E_USER_NOTICE);
                $result = $this->huntCode($_GET['code']);
                $statusTpl->setVariable("STATUS",_('Celny strzał. Kolejne trofeum do kolekcji!'));
                $tpl->setCurrentBlock('code_row');

                $tpl->setVariable("NAME",$result['name']);

                if(isSet($result['desc']))
                {
                    $tpl->setVariable("DESC",$result['desc']);
                }

                if(isSet($result['pic_file']))
                {
                    $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
                    $tpl->setVariable("URL",rtrim($global['service_url'],'/')."/send_image.php?id=".$result['pic_file']);
                    $tpl->setVariable("ALT",$result['pic_file']);
                }

                $tpl->parseCurrentBlock(); 
            }
            else
            {
                trigger_error("HuntCodeModule::getContent() Code already hunted.", E_USER_NOTICE);
                $statusTpl->setVariable("STATUS",_('Kod był już upolowany.'));      
            }
        }
        else
        {
            trigger_error("HuntCodeModule::getContent() Wrong code.", E_USER_NOTICE);
            $statusTpl->setVariable("STATUS",_('Kod nie jest poprawny. Spróbuj jeszcze raz')); 
        }

               
        
        return $statusTpl->get().$tpl->get().$statusTpl->get();
    }
    
    // }}}
    
    function isValid($code)
    {
        if ( !isSet($code) )
        {
            return false;
        }


        $sth = $this->dbh->prepare("SELECT COUNT(code) AS count ,rowid,* FROM guide WHERE code = :code AND publish = 1");
        $sth->bindParam(':code',$code);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);

        if ( $result['count'] == 1 )
        {
            return true;
        }
        elseif ( $result['count'] > 1 )
        {
            trigger_error("HuntCodeModule::isValid()  Fatal error code is duplicated !", E_USER_ERROR);
        }

        return false;
    
    }

    function isHunted($code)
    {
        if ( !isSet($code) )
        {
            trigger_error("HuntCodeModule::getContent()  Fatal error code is empty", E_USER_ERROR);
        }

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $this->dbh = new PDO($options['dsn_string']);
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $sth = $this->dbh->prepare("SELECT COUNT(hunted_code.code_id) AS count FROM guide,hunted_code WHERE hunted_code.code_id = guide.rowid AND guide.code = :code AND hunted_code.owner = :owner");
        $sth->bindParam(':code',$code);
        $sth -> bindParam(':owner',  AuthSingleton::instance('AUTH_USER')->getUsername(), PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);

        if ( $result['count'] == 0 )
        {
            return false;
        }
        elseif ( $result['count'] > 1 )
        {
            trigger_error("HuntCodeModule::isHunted()  Fatal error code is duplicated !", E_USER_ERROR);
        }

        return true;
    
    }

    function huntCode($code)
    {
        trigger_error("ENTER HuntCodeModule::huntCode()", E_USER_NOTICE);        
        if ( !isSet($code) )
        {
            trigger_error("HuntCodeModule::huntCode()  Fatal error code is empty", E_USER_ERROR);
        }
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $this->dbh = new PDO($options['dsn_string']);
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        trigger_error("HuntCodeModule::huntCode()  Select code from DB", E_USER_NOTICE);
        $sth = $this->dbh->prepare("SELECT rowid,*,
            (SELECT name FROM guide_name WHERE guide_name.locale = :locale AND guide_id = guide.rowid) AS name,
            (SELECT desc FROM guide_desc WHERE guide_desc.locale = :locale AND guide_id = guide.rowid) AS desc  
            FROM guide WHERE code = :code ");
        $sth->bindParam(':locale',  LocalizationSingleton::getLocale(), PDO::PARAM_STR);
        $sth->bindParam(':code',$code);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);

        trigger_error("HuntCodeModule::huntCode()  Insert hunted code to DB", E_USER_NOTICE);
        $sth = $this->dbh->prepare("INSERT INTO hunted_code (code_id, time, owner) VALUES (:code_id, :time, :owner) ") ; 
        $sth -> bindParam(':owner',  AuthSingleton::instance('AUTH_USER')->getUsername(), PDO::PARAM_STR);
        $sth -> bindParam(':code_id',  $result['rowid'], PDO::PARAM_INT);
        $sth -> bindParam(':time', time(), PDO::PARAM_INT);
        trigger_error("HuntCodeModule::huntCode() execute", E_USER_NOTICE);
        $sth -> execute(); 
        trigger_error("LEAVE HuntCodeModule::huntCode()", E_USER_NOTICE);        
        return $result;
 
    }

}

// }}} 

?>

